package jd.se.borrowinfo;

import jd.se.common.CurrentUser;
import jd.se.common.Result;
import jd.se.common.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet({
        "/borrowinfo/borrowinfolist",
        "/borrowinfo/lendinfolist"
})
public class BorrowInfoAPI extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        String path = request.getServletPath();
        if("/borrowinfo/borrowinfolist".equals(path)){
            CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);

            request.setCharacterEncoding("UTF-8");
            QueryBorrowDto queryBorrowDto = new QueryBorrowDto();
            try {
                queryBorrowDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
            } catch (Exception e) {}

            try {
                queryBorrowDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
            } catch (Exception e) {}

            //创建业务模型对象（服务对象/Service对象），通过业务对象执行业务方法
            BorrowInfoService borrowInfoService = new BorrowInfoService();
            Map<String,Object> page =  borrowInfoService.getInfoList(currentUser.getUserId(), queryBorrowDto);

            Utils.outResult(resp, Result.success(page));
        }

        if("/borrowinfo/lendinfolist".equals(path)){
            CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);
            System.out.println(request.getSession());
            request.setCharacterEncoding("UTF-8");
            QueryLendDto queryLendDto = new QueryLendDto();
            try {
                queryLendDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
            } catch (Exception e) {}

            try {
                queryLendDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
            } catch (Exception e) {}

            //创建业务模型对象（服务对象/Service对象），通过业务对象执行业务方法
            BorrowInfoService borrowInfoService = new BorrowInfoService();
            Map<String,Object> page =  borrowInfoService.getLendInfoList(currentUser.getUserId(), queryLendDto);

            Utils.outResult(resp, Result.success(page));
        }

    }
}
